<template>
  <div>
    <el-form :model="form" label-width="auto" inline>
      <el-form-item label="提交时间:">
        <el-date-picker v-model="times" type="datetimerange" start-placeholder="开始时间" value-format="x"
          end-placeholder="结束时间" time-format="A hh:mm:ss" />
      </el-form-item>
      <el-form-item label="收货仓库:">
        <el-select v-model="form.warehouseId">
          <el-option label="全部" :value="0"></el-option>
          <el-option :label="item.value" :value="item.key"
            v-for="item in store.productGearPurchaseListWarehouseDicTable" clearable :key="item.key" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="getList">搜索</el-button>
        <el-button @click="goodsDelivered" style="margin-left: 50px;">一键拿货完成</el-button>
        <el-button @click="printInventory">一键打印清单</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="table">
    <el-table ref="multipleTableRef" :data="tableData" style="width: 100%" @selection-change="handleSelectionChange"
      border>
      <el-table-column type="selection" width="55" />
      <el-table-column label="拿货号">
        <template #default="scope">{{ scope.row.purchaseCode }}</template>
      </el-table-column>
      <el-table-column label="收货仓库" width="180">
        <template #default="scope">{{ scope.row.warehouseName }}</template>
      </el-table-column>
      <el-table-column label="提交时间" width="180">
        <template #default="scope">{{ scope.row.createTime }}</template>
      </el-table-column>
      <el-table-column label="商品信息" width="180">
        <template #default="scope"><span style="color: blue;" class="headclass" @click="openSon(scope.row)">{{
          scope.row.skuInfo
            }}</span></template>
      </el-table-column>
      <el-table-column label="需要拿货件数" width="180">
        <template #default="scope"><span style="color: red;">{{ scope.row.purchaseNum }}</span></template>
      </el-table-column>
      <el-table-column label="拿货档口" width="180">
        <template #default="scope">{{ scope.row.gearOpenName }}</template>
      </el-table-column>
      <el-table-column label="拿货价">
        <template #default="scope">{{ scope.row.purchasePrice }} <span style="color: blue;" class="headclass"
            @click="openEvery(scope.row.productId)">更多</span> </template>
      </el-table-column>
      <el-table-column label="出库价">
        <template #default="scope">{{ scope.row.outPrice }}</template>
      </el-table-column>
      <el-table-column label="备注">
        <template #default="scope">{{ scope.row.remark }}</template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button link type="primary" size="small" @click.prevent="TakeTheGoodsCompletedSubmit
            (scope.row)">
            拿货完成
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div class="page">
    <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[10, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </div>
  <!-- <div>
    <el-dialog v-model="EverydialogVisible" width="500" :before-close="handleClose">
      <span>
        <el-table :data="productGearPurchasepriceListTAblk" stripe style="width: 100%">
          <el-table-column prop="purchasePrice" label="拿货价" width="180" />
          <el-table-column prop="purchaseTime" label="记录时间" width="180" />
          <el-table-column prop="outPrice" label="出库价" />
        </el-table>
      </span>
    </el-dialog>
  </div>
  <div>
    <el-dialog v-model="TakeTheGoodsCompleteddialogVisible" width="500" :before-close="handleClose">
      <span>
        <el-form :model="TakeTheGoodsCompletedForm" style="max-width: 600px">
          <el-form-item label="拿货价：">
            <el-input v-model="TakeTheGoodsCompletedForm.purchasePrice" />
          </el-form-item>
          <el-form-item label="出库价：">
            <el-input v-model="TakeTheGoodsCompletedForm.outPrice" />
          </el-form-item>
        </el-form>
      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="TakeTheGoodsCompleteddialogVisible = false">取消</el-button>
          <el-button type="primary" @click="TakeTheGoodsCompletedSubmit">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div> -->
  <div>
    <el-dialog v-model="EverydialogVisible" width="750">
      <div v-if="purchasePriceOr">
        <el-form :model="purchasePriceform" inline>
          <el-form-item label="出库价">
            <el-input v-model="purchasePriceform.outPrice" />
          </el-form-item>
          <el-form-item label="拿货价">
            <el-input v-model="purchasePriceform.purchasePrice" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="setpurchasePriceOnSubmit">确定</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="productGearPurchasepriceListTAblk" border style="width: 100%">
        <el-table-column prop="outPrice" label="出库价" width="120" />
        <el-table-column prop="purchasePrice" label="拿货价" width="120" />
        <el-table-column prop="purchaseTime" label="记录时间" width="220" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button link type="primary" size="small" @click.prevent="setpurchasePriceOr(scope.row)">
              修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onBeforeMount, onBeforeUnmount, onMounted } from 'vue'
import { useAlertsStore } from '@/store/index'
import { timesSevenDay } from '@/utils/sevenDayTimes'
import { ElMessage } from 'element-plus'
// productGearPurchaseUpdate
import { productGearPurchaseList, productGearPurchaseUpdate, productGearPurchasepriceList, productGearPurchasePrintList, productGearPurchaseComplete } from '@/api/index'
import { useRouter } from 'vue-router'
const router = useRouter()
const store = useAlertsStore()
const times = ref([])

const form = ref(
  {
    createTimeStart: '',
    createTimeEnd: '',
    warehouseId: 0,
    pageSize: 10,
    pageNum: 1
  }
)
onBeforeMount(() => {
  store.productGearPurchaseListWarehouseDicBtn()
  times.value = timesSevenDay()
  getList()
})
onMounted(() => {
  doConnect()
})
const tableData = ref([])
const getList = () => {
  if (times.value) {
    form.value.createTimeStart = times.value[0]
    form.value.createTimeEnd = times.value[1]
  } else {
    form.value.createTimeStart = ''
    form.value.createTimeEnd = ''
  }
  productGearPurchaseList(form.value).then(res => {
    const { data: { data } } = res
    tableData.value = data.list
    total.value = data.total
  })
}
// table
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}
// 分页
const currentPage4 = ref(1)
const pageSize4 = ref(10)
const total = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  form.value.pageSize = val
  getList()
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  form.value.pageNum = val
  getList()
}
const printInventory = () => {
  // 打印清单
  if (multipleSelection.value.length > 0) {
    const a = []
    multipleSelection.value.forEach(e => {
      a.push(e.id)
    })
    productGearPurchasePrintList({
      productGearPurchaseIds: a
    }).then(res => {
      const { data: { data } } = res
      if (localStorage.getItem('PrinterSetListJH')) {
        const lists = JSON.parse(localStorage.getItem('PrinterSetListJH'))
        // console.log(lists[0].PrinterList)
        const val = { cmd: 'print', requestID: '123458976', version: '1.0', task: { taskID: (new Date().getTime() + ''), preview: false, printer: lists[0].PrinterList, notifyMode: 'allInOne', previewType: 'pdf', documents: [{ documentID: '0123456789', contents: [{ data: { width: '210', height: '297', padding: '2', fontSize: '8', left: '2', skuInfo: data, barcodeHeight: '13' }, templateURL: 'http://yitaoweb.sc.kuaidi600.com/moban/PurchaseOrder.xml' }] }] } }
        store.socket.send(JSON.stringify(val))
      } else {
        ElMessage.error('先设置打印机')
      }
    })
  } else {
    ElMessage.error('选择数据')
  }
}

const doConnect = () => {
  console.log('d打开了不')
  store.socket = new WebSocket('ws://localhost:13528')
  // 打开Socket
  store.socket.onopen = function (event) {
    // 监听消息
    store.socket.onmessage = function (event) {
      // localStorage.setItem('printerproperties', event.data)
    }
    // 监听Socket的关闭
    store.socket.onclose = function (event) {
      // console.log('Client notified socket has closed', event)
    }
  }
}
// 销毁
onBeforeUnmount(() => {
  console.log('销毁')
  store.destroyed()
})
// const EverydialogVisible = ref(false)
// const productGearPurchasepriceListTAblk = ref([])
// const openEvery = (id) => {
//   productGearPurchasepriceList(id).then(res => {
//     const { data: { data } } = res
//     productGearPurchasepriceListTAblk.value = data
//     EverydialogVisible.value = true
//   })
// }
// const TakeTheGoodsCompleteddialogVisible = ref(false)
// const TakeTheGoodsCompletedForm = ref({})
// const TakeTheGoodsCompleted = (row) => {
//   TakeTheGoodsCompletedForm.value.purchasePrice = row.purchasePrice
//   TakeTheGoodsCompletedForm.value.productGearPurchaseIds = [row.id]
//   TakeTheGoodsCompletedForm.value.outPrice = row.outPrice
//   TakeTheGoodsCompleteddialogVisible.value = true
// }
const TakeTheGoodsCompletedSubmit = (row) => {
  // productGearPurchaseUpdate(TakeTheGoodsCompletedForm.value).then(res => {
  productGearPurchaseComplete({
    productGearPurchaseIds: [row.id]
  }).then(res => {
    router.push({ path: '/home/everyView/stallStockAwaitSet' })
  })
  // })
}
const goodsDelivered = () => {
  if (multipleSelection.value.length > 0) {
    const a = []
    multipleSelection.value.forEach(e => {
      a.push(e.id)
    })
    productGearPurchaseComplete({
      productGearPurchaseIds: a
    }).then(res => {
      ElMessage.success('操作成功')
      router.push({ path: '/home/everyView/stallStockAwaitSet' })
    })
  } else {
    ElMessage.error('选择数据')
  }
}
const EverydialogVisible = ref(false)
const productGearPurchasepriceListTAblk = ref([])
const idsOld = ref('')
const openEvery = (id) => {
  if (id) {
    idsOld.value = id
    productGearPurchasepriceList(id).then(res => {
      const { data: { data } } = res
      productGearPurchasepriceListTAblk.value = data
      EverydialogVisible.value = true
    })
  } else {
    productGearPurchasepriceList(idsOld.value).then(res => {
      const { data: { data } } = res
      productGearPurchasepriceListTAblk.value = data
      EverydialogVisible.value = true
    })
  }
}
const purchasePriceform = ref({ productGearPurchaseIds: [] })
const purchasePriceOr = ref(false)
const setpurchasePriceOr = (row) => {
  purchasePriceform.value.productGearPurchaseIds = []
  purchasePriceform.value.productGearPurchaseIds.push(row.productGearPurchaseId)
  purchasePriceOr.value = true
}
const setpurchasePriceOnSubmit = () => {
  productGearPurchaseUpdate(purchasePriceform.value).then(res => {
    ElMessage.success('修改成功')
    purchasePriceform.value = { setpurchasePriceOnSubmit: '' }
    openEvery()
    purchasePriceOr.value = false
  })
}
</script>

<style lang='less' scoped></style>
